package com.elite;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import org.htmlparser.filters.*;
import org.htmlparser.*;
import org.htmlparser.nodes.*;
import org.htmlparser.tags.*;
import org.htmlparser.util.*;
import org.htmlparser.visitors.*;

/**
 * 测试HTMLParser的使用.
 * @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
 * Creation date: 2008-1-18 - 上午11:44:22
 */
public class test4 {
    /**
     * 入口方法.
     * @param args
     * @throws Exception
     */
	
	   public  static void  copyFile(String  oldPath,  String  newPath)  {     
	       try  {     
	           int  bytesum  =  0;     
	           int  byteread  =  0;     
	           File  oldfile  =  new  File(oldPath);     
	           if  (oldfile.exists())  {  //文件存在时     
	               InputStream  inStream  =  new  FileInputStream(oldPath);  //读入原文件     
	               FileOutputStream  fs  =  new  FileOutputStream(newPath);     
	               byte[]  buffer  =  new  byte[1444];     
	               int  length;     
	               while  (  (byteread  =  inStream.read(buffer))  !=  -1)  {     
	                   bytesum  +=  byteread;  //字节数  文件大小       
	                   fs.write(buffer,  0,  byteread);     
	               }     
	               inStream.close();     
	           }     
	       }     
	       catch  (Exception  e)  {     
	           System.out.println("复制单个文件操作出错");     
	           e.printStackTrace();     
	    
	       }     
	    
	   }  
	   
    public static void main(String args[]) throws Exception {
//        String path = "http://www.blogjava.net/amigoxie";
//        URL url = new URL(path);
//        URLConnection conn = url.openConnection();
//        conn.setDoOutput(true); 
    	
//        String a="c:\\XF_QCQD_ZX_20110607.xls";
//        String b="c:\\aa\\XF_QCQD_ZX_20110607.xls";
//        copyFile(a,b);
    	
    	FileInputStream is = new FileInputStream(new File("D:/WaitingForManagerCommit/2fd750a4-458f-01a4-cee5-77cd69e4f967.htm")); 
    	ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); 
		byte[] by = new byte[512];  
		int t = is.read(by,0,by.length); 
		 while(t>0){  
			 byteOS.write(by, 0, 512);  //这里别写成t，写够512，呵呵，SB的方法对付SB的java API
		 t = is.read(by,0,by.length);  
		} 
		 byteOS.close();  
//		InputStream byteIS = new ByteArrayInputStream(byteOS.toByteArray());  
        
        InputStream inputStream = new ByteArrayInputStream(byteOS.toByteArray());  
        InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
        StringBuffer sb = new StringBuffer();
        BufferedReader in = new BufferedReader(isr);
        String inputLine;
        
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine);
            sb.append("\n");
        }
        
        String result = sb.toString();

//        readByHtml(result);
        String a=readTextAndLinkAndTitle(result);
        System.out.println("a:"+a);
    }
    
    /**
     * 按页面方式处理.解析标准的html页面
     * @param content 网页的内容
     * @throws Exception
     */
    public static void readByHtml(String content) throws Exception {
        Parser myParser;
        myParser = Parser.createParser(content, "gb2312");
        HtmlPage visitor = new HtmlPage(myParser);
        myParser.visitAllNodesWith(visitor);

        String textInPage = visitor.getTitle();
        System.out.println(textInPage);
        NodeList nodelist;
        nodelist = visitor.getBody();
        
        System.out.print(nodelist.asString().trim());
    }

    /**
     * 分别读纯文本和链接.
     * @param result 网页的内容
     * @throws Exception
     */
    public static String readTextAndLinkAndTitle(String result) throws Exception {
        Parser parser;
        NodeList nodelist;
        parser = Parser.createParser(result, "gb2312");
        NodeFilter textFilter = new NodeClassFilter(TextNode.class);
        NodeFilter linkFilter = new NodeClassFilter(LinkTag.class);
        NodeFilter titleFilter = new NodeClassFilter(TitleTag.class);
        OrFilter lastFilter = new OrFilter();
        lastFilter.setPredicates(new NodeFilter[] { textFilter, linkFilter, titleFilter });
        nodelist = parser.parse(lastFilter);
        Node[] nodes = nodelist.toNodeArray();
        String line = "";
        
        
        Node node = nodes[5];
        if (node instanceof TextNode) {
            TextNode textnode = (TextNode) node;
            line = textnode.getText();
        }else if (node instanceof LinkTag) {
            LinkTag link = (LinkTag) node;
            line = link.getLink();
        } else if (node instanceof TitleTag) {
            TitleTag titlenode = (TitleTag) node;
            line = titlenode.getTitle();
        }
        return line;
        
        
        
//        for (int i = 0; i <nodes.length; i++) {
//            Node node = nodes[i];
//            if (node instanceof TextNode) {
//                TextNode textnode = (TextNode) node;
//                line = textnode.getText();
//            }else if (node instanceof LinkTag) {
//                LinkTag link = (LinkTag) node;
//                line = link.getLink();
//            } else if (node instanceof TitleTag) {
//                TitleTag titlenode = (TitleTag) node;
//                line = titlenode.getTitle();
//            }
//            
//            if (isTrimEmpty(line))
//                continue;
//            System.out.println(i+":"+line);
//        }
    }
    
    /**
     * 去掉左右空格后字符串是否为空
     */
    public static boolean isTrimEmpty(String astr) {
        if ((null == astr) || (astr.length() == 0)) {
            return true;
        }
        if (isBlank(astr.trim())) {
            return true;
        }
        return false;
    }

    /**
     * 字符串是否为空:null或者长度为0.
     */
    public static boolean isBlank(String astr) {
        if ((null == astr) || (astr.length() == 0)) {
            return true;
        } else {
            return false;
        }
    }
}